<?php

if(!defined('CLI'))define('CLI',0);
if(!defined('DEBUG'))define('DEBUG',false);
if(!defined('CONSOLE'))define('CONSOLE',0);
if(!defined('PATH_FFWD'))define('PATH_FFWD',dirname(__FILE__).DIRECTORY_SEPARATOR);

define('APPSTRUCTCACHEFN','cache/appstruct.php');
define('BLOCKREDIRECT',isset($_GET['BLOCKREDIRECT']) || 0);
define('FORCEDICTIONARY',isset($_GET['FORCEDICTIONARY']) || 1);
define('UI_LANG','hu');	

ini_set('display_errors',true);

require_once PATH_FFWD.'core/link.php';
require_once PATH_FFWD.'prototypes/application.base.php';
require_once PATH_FFWD.'prototypes/widget.base.php';
require_once PATH_CONFIG.'database.php';


class FFWD extends BaseObject{
	static $appstruct=Array();
	static $moduleindex=Array();
	
	public function __construct(){		
		ini_set('display_errors',1);				
		try{
			$_SERVER['REQUEST_DATA']=parse_location($_SERVER['REQUEST_URI']);
		}catch(Exception $e){
			//header_redirect('/404.html?url='.urlencode($_SERVER['REQUEST_URI']));	
		}
		
		if(!isset($_SERVER['REQUEST_DATA']) || !isset($_SERVER['REQUEST_DATA'][ACTION])){	
			header('Location: '.addslashes($_SERVER['REQUEST_URI']).'index.html');
			exit;	
		}	
		
		if(substr($_SERVER['REQUEST_DATA'][APPLICATION],-4)=='_dev'){
			$_SERVER['REQUEST_DATA'][APPLICATION]=substr($_SERVER['REQUEST_DATA'][APPLICATION],0,-4);
		}
		
		$afn=PATH_APPS.$_SERVER['REQUEST_DATA'][APPLICATION].'/application.'.$_SERVER['REQUEST_DATA'][APPLICATION].'.php';
		if(!file_exists($afn))throw new Exception('Application doesnt exist: '.$_SERVER['REQUEST_DATA'][APPLICATION]);
		require_once $afn;
		
		$clsn=$_SERVER['REQUEST_DATA'][APPLICATION].'Application';
		$GLOBALS['app']=new $clsn(Array(
			'name'=>$_SERVER['REQUEST_DATA'][APPLICATION],
		));					
	}
	
	public function executeApp(){
		$GLOBALS['app']->execute(1);
	}
	
	static function getTplPathFromModuleFN($fn){
		$path_apps=PATH_APPS;
		if(substr($path_apps,0,2)=='..')$path_apps=substr($path_apps,2);
		$p=strpos($fn,$path_apps);
		if($p){
			$p+=strlen($path_apps);
			$fn=substr($fn,$p);
		};		
		if(!preg_match('/(.*)\/module\.([a-zA-Z0-9_]+)*(\.php)?/',$fn,$o))return NULL;
		return strtolower( PATH_APPS.$o[1].'/templates/'.strtolower($o[2]).'/' );
	}
	
	static function scanmodules($path){
		$r=Array();
		$dir=opendir($path);
		while($item=readdir($dir)){
			if(preg_match('/module\.([a-zA-Z0-9_]+)*(\.php)?/',$item,$o)){
				$r[$o[1]]=Array(
					'name'=>$o[1],
				);				
				if(is_dir($path.$item)){
					$r[$o[1]][MODULE]=self::scanmodules($path.$item.'/');
					if(isset($r[$o[1]][MODULE]['default'])){
						$r[$o[1]]['file']=$r[$o[1]][MODULE]['default']['file'];
					}
				}else{
					$r[$o[1]]['file']=$path.$item;
				}
				$r[$o[1]]=array_merge($r[$o[1]],self::analyzemoduledefinition($r[$o[1]]['file']));				
			}
		};
		closedir($dir);
		return $r;
	}

	
	static function createDefaultTables(){
		global $db;
		$q=file_get_contents('lib/ffwd/core/create_default_tables.sql');
		$qs=explode(';',$q);
		foreach($qs as $q)if(trim($q))$db->query($q);		
	}

};
if(is_file(APPSTRUCTCACHEFN))include APPSTRUCTCACHEFN;
